package com.briup.jz.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import com.briup.jz.bean.BaseUser;
import com.briup.jz.bean.BaseUserExample;
import com.briup.jz.dao.BaseUserMapper;
import com.briup.jz.service.IBaseUserService;
import com.briup.jz.utils.CustomerException;

@Service
public class BaseUserServiceImpl implements IBaseUserService {
	@Autowired
	private BaseUserMapper baseuserMapper;
	@Override
	public void saveOrUpdate(BaseUser baseUser) throws CustomerException {
		if(baseUser.getId()!=null) {
			baseuserMapper.updateByPrimaryKey(baseUser);
		}else {
			BaseUserExample example = new BaseUserExample();
			example.createCriteria().andUsernameEqualTo(baseUser.getUsername());
			List<BaseUser> list=baseuserMapper.selectByExample(example);
			if(list.size()>0) {
				throw new CustomerException("该用户名已存在！");
			}
			baseuserMapper.insert(baseUser);
		}
	}

	@Override
	public List<BaseUser> query(String name) {
		BaseUserExample example = new BaseUserExample();
		if(name != null) {
			example.createCriteria().andUsernameLike("%"+name+"%");
		}
		List<BaseUser> list=baseuserMapper.selectByExample(example);
		return list;
	}

	@Override
	public void deleteById(long id) throws CustomerException {
		BaseUser baseUser = baseuserMapper.selectByPrimaryKey(id);
		if(baseUser == null) {
			throw new CustomerException("要删除的用户信息不存在！");
		}
		baseuserMapper.deleteByPrimaryKey(id);
	}

	
}
